﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;
namespace DAO
{
    public class SanPhamDAO : Abstract
    {
        public static bool ThemSanPham(SanPhamDTO sanPham)
        {
            string query = "insert into SanPham (TenSanPham, GiaSanPham, SoLuongTrongKho) values ('{1}',{2},{3})"
                .Replace("{1}", sanPham.TenSanPham)
                .Replace("{2}", sanPham.GiaSanPham.ToString())
                .Replace("{3}", sanPham.SoLuongTrongKho.ToString());
            return Query(query);
        }

        public static bool SuaThongTinSanPham(SanPhamDTO sanPham)
        {
            string query = "update SanPham set TenSanPham = '{0}', GiaSanPham = {1}, SoLuongTrongKho = {2} where MaSanPham = {3} "
               .Replace("{0}", sanPham.TenSanPham)
               .Replace("{1}", sanPham.GiaSanPham.ToString())
               .Replace("{2}", sanPham.SoLuongTrongKho.ToString())
               .Replace("{3}", sanPham.MaSanPham.ToString());
            return Query(query);
        }

        public static bool XoaSanPham(int _maSP)
        {
            string query = "delete from SanPham where MaSanPham = '{0}'"
                .Replace("{0}", _maSP.ToString());
            return Query(query);
        }


        public static List<SanPhamDTO> LayDSSanPham( int maSanPham = -1, string tenSanPham = null)
        {
            OleDbConnection conn = null;
            List<SanPhamDTO> listSanPham = new List<SanPhamDTO>();
            try
            {
                conn = GetConnection();
                conn.Open();
            }
            catch
            {
                throw new Exception("Connect error!");
            }
            try
            {
                string query = "select MaSanPham, TenSanPham, GiaSanPham, SoLuongTrongKho from SanPham ";
                if (maSanPham != -1)
                    query = query + "where MaSanPham = {0}".Replace("{0}", maSanPham.ToString());
                else if (tenSanPham != null)
                    query = query + "where TenSanPham = '{1}'".Replace("{1}", tenSanPham);
                OleDbDataReader rd = new OleDbCommand(query, conn).ExecuteReader();
                while (rd.Read())
                {
                    SanPhamDTO sanPham = new SanPhamDTO();

                    sanPham.MaSanPham = rd.GetInt32(0);
                    sanPham.TenSanPham = rd.GetString(1);
                    sanPham.GiaSanPham = rd.GetInt32(2);
                    sanPham.SoLuongTrongKho = rd.GetInt32(3);
                   
                    listSanPham.Add(sanPham);
                }
            }
            catch (Exception e)
            {
                listSanPham = null;
            }

            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
            return listSanPham;
        }
    }
}
